
#include"../file_h/mainhead.h"
void print_arr(){
    int i,j;
    for(i=0;i<6;i++){
        for(j=0;j<6;j++){
            printf("%2d ",arr[i][j]);
        }
        printf("\n");
    }
}

int rand_num(int n,int m){//n用来选择方法，m用来选择范围
    if(n==0){//传入0就生成0-m的随机数
        srand((unsigned)time(NULL));
        int r= rand()%m;//生成0-m的随机数
        return r;
    }else{
        int options[2]={2,4};
        int len=sizeof(options)/sizeof(options[0]);
        srand((unsigned)time(NULL));
        int index=rand()%len;
        int r=options[index];
        return r;
    }
}


void produce(){
    int place=how_many_place();
    if(place==0){
        flag=false;
        return;
    }
    int num1=rand_num(0,place);//生成0-place的随机数
    int num2=rand_num(1,0);//生成2或4的随机数
    change_arr(num1,num2);
}

int how_many_place(){
    int i,j;
    int count=0;
    for(i=1;i<=4;i++){
        for(j=1;j<=4;j++){
            if(arr[i][j]==0){
                count++;
            }
        }
    }
    return count;
}

void change_arr(int place,int num){
    int row=0,col=0,count=place;
    for(row=1;row<=4;row++){
        for(col=1;col<=4;col++)
        if(arr[row][col]==0){
            if(count==0){
                arr[row][col]=num;
            }
            count--;
        }
    }
}